package cn.adkii.mybatis.binding;

import cn.adkii.mybatis.session.SqlSession;
import cn.hutool.core.lang.ClassScanner;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author malingdi
 * @description 映射器注册机
 * @date 2023/12/1 18:51
 */
public class MapperRegistry {

    /**
     * 将已添加的映射器代理加入到Hash Map
     */
    private final Map<Class<?>, MapperProxyFactory<?>> knowMappers = new HashMap<>();


    public <T> T getMapper(Class<?> type, SqlSession sqlSession){
        final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knowMappers.get(type);
        if (mapperProxyFactory == null){
            throw new RuntimeException("Type " + type + " is not known to the MapperRegistry.");
        }
        try {
            return mapperProxyFactory.newInstance(sqlSession);
        }catch (Exception e) {
            throw new RuntimeException("Error getting mapper instance. Cause:" + e, e);
        }
    }

    public <T> void addMapper(Class<T> type) {
        /* Mapper必须是接口才会注册 */
        if (type.isInterface()){
            if (hasMapper(type)){
                // 如果重复添加了，报错
                throw new RuntimeException("Type" + type + "is already known to the MapperRegistry.");
            }
            // 注册映射器代理工厂
            knowMappers.put(type, new MapperProxyFactory<>(type));
        }
    }

    private boolean hasMapper(Class<?> type){
        return knowMappers.containsKey(type);
    }

    public void addMappers(String packageName){
        Set<Class<?>> mapperSet = ClassScanner.scanPackage(packageName);
        for (Class<?> mapperClass : mapperSet){
            addMapper(mapperClass);
        }
    }
}
